package com.zdkj.subway.check.controller.subwayRat.conductorDifferences.shoupiaoyuan;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zdkj.common.core.util.R;
import com.zdkj.subway.check.entity.SwAuConductorNotice;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwAuConductorNoticeMapper;
import com.zdkj.subway.check.result.Result;
import com.zdkj.subway.check.service.SwAuConductorNoticeService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 线网层级确认-售票员票款差异确认-售票员票款差异确认通知
 * SW_AU_CONDUCTOR_NOTICE
 */
@RestController
@RequestMapping("/checkNew")
@Slf4j
public class SwAuConductorNoticeCtrl {

    @Resource
    private SwAuConductorNoticeService swAuConductorNoticeService;
    @Resource
    SwAuConductorNoticeMapper swAuConductorNoticeMapper;
    @Resource
    SwAuConductorNoticeController swAuConductorNoticeController;
    /**
     * 售票员票款差异通知 列表
     * @param jsonObject
     * @return
     */
    @RequestMapping("/getSwAuConductorNoticeList")
    @ApiOperation(value = "售票员票款差异通知 列表", notes = "售票员票款差异通知 列表")
    public Result getSwAuConductorNoticeList(@RequestBody JSONObject jsonObject){
        return swAuConductorNoticeService.getSwAuConductorNoticeList(jsonObject);
    }

    @PostMapping("/subAuditSwAuConductorNotice")
    @ApiOperation(value = "售票员提交", notes = "售票员提交")
    public Result subAuditSwAuConductorNotice(@RequestBody SwAuConductorNotice swAuConductorNotice){
        return swAuConductorNoticeService.subAuditSwAuConductorNotice(swAuConductorNotice.getId());
    }

    @PostMapping("/stationMasterAuditSwAuConductorNotice")
    @ApiOperation(value = "审核", notes = "审核")
    public Result stationMasterAuditSwAuConductorNotice(@RequestBody SwAuConductorNotice swAuConductorNotice){
        return swAuConductorNoticeService.stationMasterAuditSwAuConductorNotice(swAuConductorNotice);
    }

    /**
     * 售票员票款差异通知 单独查询
     * @param jsonObject
     * @return
     */
    @RequestMapping("/getSwAuConductorNotice")
    @ApiOperation(value = "售票员票款差异通知 单独查询", notes = "售票员票款差异通知 单独查询")
    public Result getSwAuConductorNotice(@RequestBody JSONObject jsonObject){
        return swAuConductorNoticeService.getSwAuConductorNotice(jsonObject);
    }

    /**
     * 售票员票款差异通知 申诉
     * @param jsonObject
     * @return
     */
    @RequestMapping("/updateSwAuConductorNotice")
    @ApiOperation(value = "售票员票款差异通知 申诉", notes = "售票员票款差异通知 申诉")
    public Result updateSwAuConductorNotice(@RequestBody JSONObject jsonObject){
        return swAuConductorNoticeService.updateSwAuConductorNotice(jsonObject);
    }

    /**
     * 售票员票款差异通知 上传附件
     * @param file
     * @return
     */
    @RequestMapping("/uploadFilezhanNeiRYDLCK")
    @ApiOperation(value = "售票员票款差异通知 上传附件", notes = "售票员票款差异通知 上传附件")
    public Result uploadFilezhanNeiRYDLCK(@RequestParam MultipartFile file) throws IOException {
        Result res=new Result(ResultDetailMsg.OK);
        try {
            String path="/data/minio/data/zdkj/"+file.getOriginalFilename();
            file.transferTo(new File(path));
        }catch (Exception e){
            log.error("{}",e);
            res.setMsg("上传异常");
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
        }
        return res;
    }

    /**
     * 售票员票款差异通知 撤回申诉
     * @param ids
     * @return
     */
    @RequestMapping("/recallAppealSwAuConductorNotice")
    @ApiOperation(value = "售票员票款差异通知 撤回申诉", notes = "售票员票款差异通知 撤回申诉")
    public Result recallAppealSwAuConductorNotice(String ids){
        Result res=new Result(ResultDetailMsg.OK);
        try {
            List<SwAuConductorNotice> swAuConductorNotices = swAuConductorNoticeMapper.selectBatchIds(Arrays.asList(ids.split(",")));
            for(SwAuConductorNotice swAuConductorNotice:swAuConductorNotices){
                SwAuConductorNotice s=new SwAuConductorNotice();
                s.setId(swAuConductorNotice.getId());
                s.setFileUrl("");
                s.setAppealStatus("1");
                s.setAppealType("");
                s.setRemark("");
                s.setAppealReason("");
                s.setOvertimeReason("");
                swAuConductorNoticeMapper.updateById(s);
            }
        }catch (Exception e){
            log.error("{}",e);
            res.setMsg("上传异常");
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
        }
        return res;
    }

    /**
     * 确认
     * @param id
     * @return
     */
    @RequestMapping("/affirmNotice")
    @ApiOperation(value = "售确认", notes = "确认")
    public R affirmNotice(Integer id, String remark){
        SwAuConductorNotice swAuConductorNotice = swAuConductorNoticeMapper.selectById(id);
        //获取售票员名称
        Map<String,String> name = swAuConductorNoticeMapper.selUser(swAuConductorNotice.getCreatorName());
        SwAuConductorNotice s=new SwAuConductorNotice();
        s.setId(id);
        //申诉状态
        s.setAppealStatus("6");
        //审核状态
        s.setAuditSate("2");
        s.setApproverName(name.get("nikename"));
        s.setApproverJob(swAuConductorNotice.getCreatorName());
        s.setApproverTime(new Date());
        s.setRemark(remark);
        return R.ok(swAuConductorNoticeController.setAppealReply(String.valueOf(id),s));
    }


    @RequestMapping("/recallAffirm")
    @ApiOperation(value = "撤回确认", notes = "撤回确认")
    public R recallAffirm(Integer id){
        SwAuConductorNotice s=new SwAuConductorNotice();
        s.setId(id);
        //申诉状态
        s.setAppealStatus("1");
        //审核状态
        s.setAuditSate("1");
        s.setApproverName(null);
        s.setApproverJob(null);
        s.setApproverTime(null);
        s.setRemark(null);
        swAuConductorNoticeMapper.updateById(s);
        return R.ok();
    }
}
